<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

aos_header( 'Buddy' );

$buddy_id = formatInput( $_GET[ 'id' ] );
if( isset( $buddy_id ) && is_numeric( $buddy_id ) ) {
?>
					<div class="blok">
<?php
printDiplomacyTab( 'Buddies' );
?> 
					</div>
					<br />
    				<div class="blok">
    					<?php transprint( 'Back to' ); ?> <a href="buddylist.php"><?php transprint( 'buddylist' ); ?></a><br />
    					<br />
<?php	
	$query = "SELECT * FROM buddy WHERE buddy_id = $buddy_id";
	$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	if( ( $row2[ 'user_id1' ] == $_SESSION[ 'user_id' ] || $row2[ 'user_id2' ] == $_SESSION[ 'user_id' ] ) && $row2[ 'accept1' ] == 1 && $row2[ 'accept2' ] == 1 ) {
		$other_user_id = $row2[ 'user_id1' ];
		$number = 1;
		if( $other_user_id == $_SESSION[ 'user_id' ] ) {
			$other_user_id = $row2[ 'user_id2' ];
			$number = 2;
		}
		$query = "SELECT username, email, info FROM users WHERE user_id = $other_user_id";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		print( translate( "Username" ) . ": " . $row[ 'username' ] . "<br />\r\n" );
		$query3 = "SELECT count(*) FROM buddy WHERE ( user_id1 = $other_user_id OR user_id2 = $other_user_id ) AND accept1 = 1 AND accept2 = 1";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		print( translate( "Buddies" ) . ": " . number_format( $row3[ 'count(*)' ] ) . "<br />\r\n" );
		if( $number == 1 ) {
			if( $row2[ 'ruler1' ] == 1 ) {
				$query3 = "SELECT name FROM ruler WHERE user_id = $other_user_id";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( isset( $row3[ 'name' ] ) ) {
					print( translate( "Ruler" ) . ": <a href=\"../overview/ruler.php?name=" . $row3[ 'name' ] . "\">" . $row3[ 'name' ] . "</a><br />\r\n" );
				}
				else {
					print( translate( "Ruler" ) . ": " . translate( 'Unknown' ) . "<br />\r\n" );
				}
			}
			if( $row2[ 'email1' ] == 1 ) {
				print( translate( "E-mail" ) . ": <a href=\"mailto:" . $row[ 'email' ] . "\">" . $row[ 'email' ] . "</a><br />\r\n" );
			}
			if( $row2[ 'info1' ] == 1 && $row[ 'info' ] != '' ) {
				print( "<pre>" . htmlentities( stripslashes( $row[ 'info' ] ) ) . "</pre><br />\r\n" );
			}
			else {
				print( "<br />" );
			}
		}
		else {
			if( $row2[ 'ruler2' ] == 1 ) {
				$query3 = "SELECT name FROM ruler WHERE user_id = $other_user_id";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( isset( $row3[ 'name' ] ) ) {
					print( translate( "Ruler" ) . ": <a href=\"../overview/ruler.php?name=" . $row3[ 'name' ] . "\">" . $row3[ 'name' ] . "</a><br />\r\n" );
				}
				else {
					print( translate( "Ruler" ) . ": " . translate( "Unknown" ) . "<br />\r\n" );
				}
			}
			if( $row2[ 'email2' ] == 1 ) {
				print( translate( "E-mail" ) . ": <a href=\"mailto:" . $row[ 'email' ] . "\">" . $row[ 'email' ] . "</a><br />\r\n" );
			}
			if( $row2[ 'info2' ] == 1 && $row[ 'info' ] != '' ) {
				print( "<pre>" . $row[ 'info' ] . "</pre><br />\r\n" );
			}
			else {
				print( "<br />" );
			}
		}
		
		$query = "SELECT * FROM buddy WHERE ( user_id1 = $other_user_id OR user_id2 = $other_user_id ) AND accept1 = 1 AND accept2 = 1 ";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		
		$no_buddies = true;
		
		$i = 0;
		while ( $i < $num ) {
			if( !( mysql_result( $result, $i, "user_id1" ) == $_SESSION[ 'user_id' ] && mysql_result( $result, $i, "user_id2" ) == $other_user_id ) 
				&& ( !( mysql_result( $result, $i, "user_id2" ) == $_SESSION[ 'user_id' ] && mysql_result( $result, $i, "user_id1" ) == $other_user_id ) ) ) {
					
				$other_id = mysql_result( $result, $i, "user_id1" );
				if( $other_id == $other_user_id ) {
					$other_id = mysql_result( $result, $i, "user_id2" );
				}
				
				$query3 = "SELECT * FROM buddy WHERE ( ( user_id1 = " . $_SESSION[ 'user_id' ] . " AND user_id2 = $other_id ) OR ( user_id2 = " . $_SESSION[ 'user_id' ] . " AND user_id1 = $other_id ) ) AND accept1 = 1 AND accept2 = 1";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				
				if( isset( $row3[ 'buddy_id' ] ) && ( ( $row3[ 'user_id1' ] == $other_id && $row3[ 'ruler1'] == 1 ) || ( $row3[ 'user_id2' ] == $other_id && $row3[ 'ruler2'] == 1 ) ) ) {
					$this_buddy = $row3[ 'buddy_id' ];
					
					if( $no_buddies ) {
						print( "<strong>" . translate( "Mutual buddies" ) . ":</strong><br />\r\n" );
						print( "<ul>" );
						$no_buddies = false;
					}
				
					$query3 = "SELECT name FROM ruler WHERE user_id = $other_id";
					$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
					$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
					if( isset( $row3[ 'name' ] ) ) {
						print( "<li><a href=\"buddy.php?id=" . $this_buddy . "\">" . $row3[ 'name' ] . "</a></li>" );
					}
					else {
						print( "<li><a href=\"buddy.php?id=" . $this_buddy . "\">" . translate( "Unknown" ) . "</a></li>" );
					}
				}
			}
			$i ++;
		}
		
		if( $no_buddies ) {
			transprint( "You have no mutual buddies." );
		}
		else {
			print( "</ul>" );
		}
		
	}
	else {
	    transprint( "No valid buddy id!" );
	}
}
else {
?>
					<div class="blok">
<?php
printDiplomacyTab( 'Buddies' );
?> 
					</div>
					<br />
    				<div class="blok">
    					<?php transprint( 'Back to' ); ?> <a href="buddylist.php"><?php transprint( 'buddylist' ); ?></a><br />
    					<br />
    					<?php transprint( 'No valid buddy id.' ); ?>
    				</div>
<?php
}
aos_footer();
?>